#!/usr/local/bin bash
src=$(pwd)/src
projectpath=$(pwd)
gopath=${projectpath}:/Users/lichao/project/golang
binDir=$(pwd)/bin

function install()
{
    if [ -d "$src/$1" ]; then
        #mac
        if [ "Darwin" = $(uname) ]; then
            echo "cd ${src}/$1/ && GOPATH=\"${gopath}\" go install"
            cd ${src}/$1/ && GOPATH="${gopath}" go install
            mkdir -p ${binDir}/darwin_amd64/
            mv ${binDir}/git-agent ${binDir}/darwin_amd64/
            cp -rf ${src}/$1/config/config.toml ${binDir}/darwin_amd64
        fi

        #linux
        echo "cd ${src}/$1/ && GOPATH=\"${gopath}\" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install"
        cd ${src}/$1/ && GOPATH="${gopath}" CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go install
        cp -rf ${src}/$1/config/config.toml ${binDir}/linux_amd64
    else
        echo "$src/$1 not exists"
    fi
}

if [ $1 ]; then
    install $1
else
  # shellcheck disable=SC2045
  for srv in `ls $src`
  do
    install $srv
  done
fi